/*
 * @(#)InvokerManagerTestCase.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.invoker;

import static org.junit.Assert.*;

import javax.servlet.ServletContext;

import junit.framework.Assert;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockServletContext;

import com.nhncorp.lucy.common.config.model.ApplicationInfo;
import com.nhncorp.lucy.common.container.SpringBeanFactory;
import com.nhncorp.usf.core.config.ConnectorInfos;
import com.nhncorp.usf.core.config.runtime.Action;
import com.nhncorp.usf.core.exception.DefaultExceptionHandler;
import com.nhncorp.usf.core.exception.ExceptionHandler;
import com.nhncorp.usf.core.service.DefaultServiceExecutor;
import com.nhncorp.usf.core.service.ServiceExecutor;
import com.nhncorp.usf.core.servlet.ServletContextHolder;
import com.nhncorp.usf.core.util.ScriptUtil;

/**
 * @author Web Platform Development Team
 */
public class InvokerManagerTestCase {
    static ServiceExecutor serviceExecutor = new DefaultServiceExecutor();
    static javax.script.ScriptEngineManager scriptEngineManager = new javax.script.ScriptEngineManager();
    static Action actionInfo;

    @Before
    public void initialize() throws Exception {

        ApplicationInfo.setFileEncoding("utf-8");
        Invoker invoker = new SpringInvoker();
        ConnectorHolder connectorHolder = new ConnectorHolder(invoker);
        ExceptionHandler exceptionHandler = new DefaultExceptionHandler();
        serviceExecutor.setInvoker(invoker);
        serviceExecutor.setExceptionHandler(exceptionHandler);


        InvocationConnectorManager.getInstance().putInvoker(
                "localSpring",
                connectorHolder
        );

        ConnectorInfos.getInstance().setDefaultConnector("localSpring");
        ServletContext servletContext = new MockServletContext();
        ServletContextHolder.set(servletContext);
        String scriptDirPath = servletContext.getRealPath("/scripts");
        ServletContextHolder.setContextRealPath(scriptDirPath);
        ScriptUtil.loadJSFile(scriptDirPath);
    }

    @AfterClass
    public static void destroy() throws Exception {
        SpringBeanFactory.clear();
        InvocationConnectorManager.getInstance().putInvoker("localSpring", (Invoker) null);
    }

    @Test
    public void discardTest() throws Exception {
        try {
            InvocationConnectorManager.getInstance().discard();
        } catch (Exception e) {
            Assert.fail();
        }
    }

    @Test
    public void getInvokerErrorTest() throws Exception {
        try {
            InvocationConnectorManager.getInstance().putInvoker("localSpring", (Invoker) null);
            InvocationConnectorManager.getInstance().getRBConnectorHolder("test");
        } catch (IllegalArgumentException e) {
            assertEquals("Default Invoker(localSpring) does not exist.", e.getMessage());
        }
    }
}
